﻿using System;

namespace FlightToolkit.SimProp
{
    internal class PropertySetDescriptor : System.ComponentModel.PropertyDescriptor
    {
        private PropertySet _owner;
        private PropertySet _propertySet;

        public PropertySetDescriptor(PropertySet owner, PropertySet propertySet)
            : base(propertySet.Name, null)
        {
            this._propertySet = propertySet;
            this._owner = owner;
        }

        public override bool Equals(object obj)
        {
            var other = obj as PropertySetDescriptor;
            return other != null && other._propertySet.Name.Equals(this._propertySet.Name);
        }

        public override int GetHashCode() { return this._propertySet.Name.GetHashCode(); }

        public override bool IsReadOnly { get { return false; } }

        public override void ResetValue(object component) { }

        public override bool CanResetValue(object component) { return false; }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override System.Type ComponentType
        {
            get { return _owner.GetType(); }
        }

        public override System.Type PropertyType
        {
            get
            {
                return typeof(PropertySet);
            }
        }

        public override object GetValue(object component)
        {
            return this._propertySet;
        }

        public override void SetValue(object component, object value)
        {
            OnValueChanged(component, EventArgs.Empty);
            this._propertySet = (PropertySet)value;
        }
    }
}
